package com.ylh.user.controller;

import com.ylh.core.BaseReturnVo;
import com.ylh.core.Code;
import com.ylh.user.bo.UserBO;
import com.ylh.user.entity.UserDO;
import com.ylh.user.service.UserService;
import com.ylh.user.vo.UserVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName UserController
 * @Description
 * @Author yanglh
 * @Date 2020/2/21 22:53
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private Code code;

    @RequestMapping("/queryUser")
    public BaseReturnVo<UserBO> queryUser(@RequestParam String name, @RequestParam String password){
        BaseReturnVo<UserBO> baseReturnVo = new BaseReturnVo<>();
        UserBO userBO = userService.getUser(name,password);
        if(userBO != null){
            baseReturnVo.setFlag("1").setErrMsg("成功").setData(userBO);
        }else {
            baseReturnVo.setFlag("0").setErrCode("10000").setErrMsg("用户名或密码不正确");
        }
        return baseReturnVo;
    }

    @RequestMapping("/register")
    public BaseReturnVo register(@RequestBody UserVO userVo) {
        BaseReturnVo<UserDO> baseReturnVo = new BaseReturnVo<>();
        userService.saveUser(userVo);
        baseReturnVo.setFlag("1").setErrMsg("成功");
        return baseReturnVo;
    }

    @RequestMapping("/test")
    public Code test() {
        return code;
    }
}
